<template>
    <el-dialog
            :title="!dataForm.invoiceId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                <el-form-item label="发票类型" prop="invoiceType">
                        <el-input v-model="dataForm.invoiceType" placeholder="发票类型"></el-input>
                </el-form-item>
                <el-form-item label="发票号码" prop="invoiceNumber">
                        <el-input v-model="dataForm.invoiceNumber" placeholder="发票号码"></el-input>
                </el-form-item>
                <el-form-item label="开票日期" prop="invoiceDate">
                        <el-date-picker
                                v-model="dataForm.invoiceDate"
                                type="datetime"
                                placeholder="开票日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="开票金额" prop="invoiceAmount">
                        <el-input v-model="dataForm.invoiceAmount" placeholder="开票金额"></el-input>
                </el-form-item>
                 <el-form-item label="发票状态" prop="invoiceStatus">
                    <el-select v-model="dataForm.invoiceStatus" placeholder="请选择发票状态">
                        <el-option label="已开具" value="pending"></el-option>
                        <el-option label="已打印" value="completed"></el-option>
                        <el-option label="已发送" value="cancelled"></el-option>
                    </el-select>
                </el-form-item>  
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            invoiceId: null,
            taskId: null,
            invoiceType: null,
            invoiceNumber: null,
            invoiceDate: null,
            invoiceAmount: null,
            invoiceStatus: null
          },
          dataRule: {
            invoiceId: [
                        { required: true, message: '发票ID，主键不能为空', trigger: 'blur' }
            ],
            taskId: [
                        { required: true, message: '任务单ID，外键关联任务单表不能为空', trigger: 'blur' }
            ],
            invoiceType: [
                        { required: true, message: '发票类型（如：增值税专用发票、普通发票）不能为空', trigger: 'blur' }
            ],
            invoiceNumber: [
                        { required: true, message: '发票号码不能为空', trigger: 'blur' }
            ],
            invoiceDate: [
                        { required: true, message: '开票日期不能为空', trigger: 'blur' }
            ],
            invoiceAmount: [
                        { required: true, message: '开票金额不能为空', trigger: 'blur' }
            ],
            invoiceStatus: [
                        { required: true, message: '发票状态（如：已开具、已打印、已发送）不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.invoiceId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.invoiceId = id || 0
            if (this.dataForm.invoiceId) {
              this.$http({
                url: this.$http.adornUrl(`/app/invoices/info/${this.dataForm.invoiceId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appinvoices
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/invoices/${!this.dataForm.invoiceId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>